package com.example;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * Created by IntelliJ IDEA.
 * User: Magnus
 * Date: 2012-01-29
 * Time: 00:15
 * To change this template use File | Settings | File Templates.
 */
public class SoundThread {//extends Thread {
    private SoundPool soundPool;
    private int soundId;
    private int soundId2;
    private long sleepTime;
    float volume;

    private boolean accentuateFirstBeat = true;
    private int beatCount = 0;
    private boolean stopped = true;

    //Time signature
    private int numberOfBeatsInBar = 4;
    private int beatUnit = 4;



    /*long startTime;
    float actionTime;
    long endTime;*/
    
    public SoundThread(SoundPool soundPool, AudioManager audioManager, int soundId, int soundId2, boolean accentuateFirstBeat) {
        this.soundPool = soundPool;

        // Getting the user sound settings
        float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        volume = actualVolume / maxVolume;

        this.soundId = soundId;
        this.soundId2 = soundId2;
        this.accentuateFirstBeat = accentuateFirstBeat;
    }
    
    //@Override
    public void run() {

        //startTime = System.nanoTime();
        if (!stopped) {
            if (accentuateFirstBeat && beatCount == 0)
                playSound(soundId2);
            else
                playSound(soundId);

            //playSound(rate);

            beatCount++;
            beatCount %= numberOfBeatsInBar;
        }
        /*else {
            beatCount = 0;
        }*/
        //endTime = System.nanoTime();
        //actionTime =(endTime - startTime) * (0.000001f);

        mHandler.sendMessageDelayed(new Message(), sleepTime);// - Math.round(actionTime));
    }
    
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            run();
        }
    };
    
    public boolean isRunning() {
        return !stopped;
    }
    
    public void toggle() {
        stopped = !stopped;
    }
    
    public void setTempo(int tempo) {
        // Calculated as 1000 / (tempo/60)
        //TODO: Our metronome is slight slower than Cubase... rounding error?
        sleepTime = Math.round((60000.0 / tempo)/(beatUnit/4));
    }

    public void setAccentuateFirstBeat(boolean accentuateFirstBeat) {
        this.accentuateFirstBeat = accentuateFirstBeat;
    }

    public void setNumberOfBeatsInBar(int numberOfBeatsInBar) {
        this.numberOfBeatsInBar = numberOfBeatsInBar;
    }

    public void setBeatUnit(int beatUnit) {
        this.beatUnit = beatUnit;
    }
    
    private void playSound(int soundId) {
        soundPool.play(soundId, volume, volume, 100, 0, 1);
    }
            // Is the sound loaded already?
            //if (loaded)
            // {
            //soundPool.setLoop(soundID, -1);
            

            //Log.e("Test", "Played sound");
            // }

}
